//
//  XQListView.swift
//  XQSwiftUIDemo
//
//  Created by sinking on 2019/11/16.
//  Copyright © 2019 WXQ. All rights reserved.
//

import SwiftUI

struct XQListView: View {
    
    /// 一般静态数据, 用 @State, 动态用 @ObservedObject
    @ObservedObject var dataObj = XQListViewData.init(dataArr: xq_dataArr)
    
    var body: some View {
        
        // list 就放在最外层, 类似作为一个描述
        List {
            
            /// 想要 section 效果
            /// 就需要 ForEach 包裹 Section
            /// 然后 Section 里面再 ForEach
            ///
            /// 一般情况下, 不需要Section, 都是 List { ForEach { } } 就可以了
            ///
            ForEach.init(self.dataObj.sectionDataArr) { sectionItem in
                Section (
                    header: XQListHeaderView(dataObj: self.dataObj, sectionItem: sectionItem)
                    ,
                    
                    footer: NavigationLink.init(destination: Text("这是尾部"), label: {
                        Text("这是尾部")
                    }),
                    
                    content: {
                        
                        ForEach.init(sectionItem.datas) { item in
                            // 分区里面具体的行
                            NavigationLink(destination: Text(item.name), label: {
                                HStack {
                                    Text(item.name)
                                }
                                .padding(.all, 0)
                            })
                                .deleteDisabled(item.deleteDisabled)
                                .moveDisabled(item.moveDisabled)
                                .padding(.all, 0)
                            
                        }
                            /// 需要 ForEach 才能有 onDelete
                            .onDelete { (indexSet) in
                                sectionItem.datas.remove(at: indexSet.first!)
                                self.dataObj.objectWillChange.send()
                        }
                        .onMove { (sourceIndex, destination) in
                            sectionItem.datas.move(fromOffsets: sourceIndex, toOffset: destination)
                            self.dataObj.objectWillChange.send()
                        }
                        
                })
                
            }
            
        }
            // style
            .listStyle(PlainListStyle.init())
            .navigationBarItems(trailing:
                HStack {
                    Button.init(action: {
                        
                        let model = XQListViewDataSectionModel.init(name: "添加\(Date())")
                        model.deleteDisabled = false
                        model.moveDisabled = false
                        self.dataObj.sectionDataArr.append(model)
                        
                    }, label: {
                        Text("添加Section")
                    })
                    EditButton()
                }
        )
            .navigationBarTitle("List", displayMode: .large)
            .onAppear {
                print("视图出现")
        }
        .onDisappear {
            print("视图消失")
        }
        
    }
}

/// listview 头部
struct XQListHeaderView: View {
    
    @ObservedObject var dataObj: XQListViewData
    @ObservedObject var sectionItem: XQListViewDataSectionModel
    
    var body: some View {
        HStack {
            NavigationLink.init(destination: Text(self.sectionItem.name), label: {
                Text(self.sectionItem.name)
                    .foregroundColor(Color.black)
                    .padding(.leading, 10)
            })
            
            Spacer()
            
            if !sectionItem.deleteDisabled {
                Button.init("删除Section") {
                    
                    let model = XQListViewDataCellModel.init(name: "添加row\(Date())")
                    model.deleteDisabled = false
                    model.moveDisabled = false
                    self.sectionItem.datas.append(model)
                    
                    let index = self.dataObj.sectionDataArr.lastIndex { (model) -> Bool in
                        if model.id == self.sectionItem.id {
                            return true
                        }
                        return false
                    }
                    
                    if let index = index {
                        self.dataObj.sectionDataArr.remove(at: index)
                    }
                    
                }
            }
            
            Button.init("添加Row") {
                let model = XQListViewDataCellModel.init(name: "添加row\(Date())")
                model.deleteDisabled = false
                model.moveDisabled = false
                self.sectionItem.datas.append(model)
                self.dataObj.objectWillChange.send()
            }
            
        }
        
    }
    
}

struct XQListView_Previews: PreviewProvider {
    static var previews: some View {
        XQListView()
    }
}

fileprivate var xq_dataArr: [XQListViewDataSectionModel] = [
    XQListViewDataSectionModel(name: "头部, 默认, 不可删除，不可移动", datas: [XQListViewDataCellModel(name: "默认, 不可删除，不可移动")])
]








